/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.functions;

import name.kudryashov.base.Argument;

public class ComposablePredicate2<A1, A2> implements Predicate2<A1, A2> {

    private final Predicate2<A1, A2> f;

    public ComposablePredicate2(Predicate2<A1, A2> f) {
        this.f = Argument.requireNotNull(f, "f");
    }

    public boolean apply(A1 arg1, A2 arg2) {
        return f.apply(arg1, arg2);
    }

    public String toString() {
        return f.toString();
    }

    public Predicate<A2> bind1(final A1 arg1) {
        return new Predicate<A2>() {
            public boolean apply(A2 arg2) {
                return f.apply(arg1, arg2);
            }

            public String toString() {
                return String.format("%s(%s, _)", f, arg1);
            }
        };
    }

    public Predicate<A1> bind2(final A2 arg2) {
        return new Predicate<A1>() {
            public boolean apply(A1 arg1) {
                return f.apply(arg1, arg2);
            }

            public String toString() {
                return String.format("%s(_, %s)", f, arg2);
            }
        };
    }
}